package lz.controls
{
	import flash.display.DisplayObject;
	import flash.display.InteractiveObject;
	import flash.events.MouseEvent;
	import flash.text.TextFormat;
	
	import lz.controls.containers.BoxDirection;
	import lz.controls.core.InvalidationType;
	import lz.controls.core.UIComponent;
	import lz.controls.events.ItemClickEvent;

	/**
	 * ButtonBar 控件可定义一组具有普通外观和导航的逻辑上相关的按压按钮，这组按钮可以是水平的也可以是垂直的。
	 * 按压按钮是一种在被选中后不会记住其选中状态的按钮。
	 * 按钮栏中按压按钮的典型用途是将一组相关按钮组合到一起，提供共同的外观和导航，并在一个位置处理 click 事件的逻辑。 
	 * @author lake
	 * 
	 */	
	public class ButtonBar extends UIComponent
	{
		/**
		 * 数据
		 */
		private var _dataProvider :Object;
		/**
		 *选择的索引 
		 */		
		private var _selectedIndex:int = -1;
		/**
		 *按钮组方向
		 * 默认水平方向 
		 */		
		private var _direction		:String = BoxDirection.HORIZONTAL;
		/**
		 * 默认样式
		 */
		private static var defaultStyles:Object={horizontalGap:2,verticalGap:2};
		/**
		 * 检索当前组件的默认样式映射。
		 */
		public static function getStyleDefinition():Object
		{
			return mergeStyles(UIComponent.getStyleDefinition(), defaultStyles);
		}
		public function ButtonBar()
		{
			super();
		}
		/**
		 *用于填充导航器控件的数据
		 * @return 
		 * 
		 */		
		public function get dataProvider():Object
		{
			return _dataProvider;
		}
		public function set dataProvider(value:Object):void
		{
			_dataProvider = value;
			var itemListArr :Array = _dataProvider as Array;
			var i :int = 0;
			var len :int = itemListArr.length;
			var itemObj :Object;
			for(i = 0;i < len;i++)
			{
				itemObj = itemListArr[i];
				createNavItem(itemObj.label,itemObj.textFormat);
			}
		}
		/**
		 *设置获取组件方向 
		 * @param value
		 * 
		 */		
		public function set direction(value:String):void
		{
			_direction = value;
		}
		public function get direction():String
		{
			return _direction;
		}
		/**
		 *设置获取选择索引 
		 * @return 
		 * 
		 */		
		public function get selectedIndex():int
		{
			return _selectedIndex;
		}
		public function set selectedIndex(value:int):void
		{
			_selectedIndex = value;
		}
		/**
		 * 创建导航按钮
		 * @param label
		 * @param icon
		 * @return 
		 * 
		 */		
		protected function createNavItem(
			label:String,
			textFormat:TextFormat = null,
			icon:Class = null):Button
		{
			var newButton:Button = new Button();
			newButton.focusEnabled = false;
			newButton.label = label;
			newButton.setSize(72,24);
			newButton.setStyle("icon", icon);
			if(textFormat)
			{
				newButton.setStyle("textFormat", textFormat);
			}
			newButton.addEventListener(MouseEvent.CLICK, clickHandler);
			addChild(newButton);
			return newButton;
		}
		/**
		 *  @private
		 */
		protected function clickHandler(event:MouseEvent):void
		{
			var index:int = getChildIndex(DisplayObject(event.currentTarget));
			_selectedIndex = index;
			var newEvent:ItemClickEvent =
				new ItemClickEvent(ItemClickEvent.ITEM_CLICK);
			newEvent.label = Button(event.currentTarget).label;
			newEvent.index = index;
			newEvent.relatedObject = InteractiveObject(event.currentTarget);
			/*newEvent.item = _dataProvider ?
				_dataProvider.getItemAt(index) :
				null;*/
			dispatchEvent(newEvent);
			event.stopImmediatePropagation();
		}
		/**
		 * 画界面
		 */		
		override protected function draw():void
		{
			if (isInvalid(InvalidationType.STYLES)) //样式变化
			{
				drawGap();
			}
		}
		/**
		 *间隔变化 
		 */		
		protected function drawGap():void
		{
			var i 				:int = 0;
			var n 				:int = numChildren;
			var btn 			:Button;
			var horizontalGap 	:Number = Number(getStyleValue("horizontalGap"));
			var verticalGap 	:Number = Number(getStyleValue("verticalGap"));
			var totalHorizontalX:Number = 0;
			var totalVerticalY	:Number = 0;
			for(i = 0;i < n;i++)
			{
				btn = Button(getChildAt(i));
				if(_direction == BoxDirection.HORIZONTAL)
				{
					btn.x = totalHorizontalX;
					btn.y = 0;
					totalHorizontalX += (btn.width + horizontalGap);
				}
				else
				{
					btn.y = totalVerticalY;
					btn.x = 0;
					totalVerticalY += (btn.height + verticalGap);
				}
			}
		}
	}
}